package com.xiong.springcloud.controller;

import com.xiong.springcloud.entities.Payment;
import com.xiong.springcloud.result.CodeMsg;
import com.xiong.springcloud.result.Result;
import com.xiong.springcloud.service.impl.PaymentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author wardseptember
 * @create 2020-03-27 17:01
 */
@RestController
@Slf4j
public class PaymentController {
    @Autowired
    private PaymentServiceImpl paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/payment/create")
    public Result<Payment> create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("插入结果"+result);
        if (result > 0) {
            log.info("serverPort: "+serverPort);
            return Result.success(payment);
        } else {
            return Result.error(CodeMsg.INSERT_FAILED);
        }

    }

    @GetMapping("/payment/get/{id}")
    public Result<Payment> getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询结果"+payment);
        if (payment != null) {
            log.info("serverPort: "+serverPort);
            return new Result<>(200, serverPort, payment);
//            return Result.success(payment);
        } else {
            return Result.error(CodeMsg.SELECT_FAILED);
        }
    }

    @GetMapping("/payment/lb")
    public String getPaymentLB() {
        return serverPort;
    }
}
